package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import persistence.PersistenceEvento;
import persistence.PersistenceUsuario;
import model.Evento;
import model.Usuario;

@WebServlet("/ServletCadastraEvento")
public class ServletCadastraEvento extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

    public ServletCadastraEvento() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Evento evento = requestToEvento(request);
		
		HttpSession session = request.getSession();
		session.setAttribute("evento", evento);
		
		PersistenceEvento.inserirEvento(evento);
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("/menu.jsp");
		dispatcher.forward(request, response);
	}

	private Evento requestToEvento(HttpServletRequest request) {
		String nomeEvento = request.getParameter("evento");
		String localEvento = request.getParameter("local");
		String iniEvento = request.getParameter("hr_inicio");
		String fimEvento = request.getParameter("hr_final");
		String dataIniEvento = request.getParameter("data_inicio");
		String dataFimEvento = request.getParameter("data_final");
			
		Evento evento = new Evento(nomeEvento, localEvento, iniEvento, fimEvento, dataIniEvento, dataFimEvento);
		return evento;
	}

}
